package com.kabigon.handler;

import com.kabigon.message.SendResponse;
import com.kabigon.message.SendToOneRequest;
import com.kabigon.message.SendToUserReqeust;
import com.kabigon.utils.WebSocketUtil;
import org.springframework.stereotype.Component;

import javax.websocket.Session;

/**
 * @author xu.biao
 * @version v1.0
 * @Package com.kabigon.handler
 * @data 2023/3/10 2:43
 */
@Component
public class SendToOneHandler implements MessageHandler<SendToOneRequest> {
    @Override
    public void execute(Session session, SendToOneRequest message) {
        //    这里假装成功
        SendResponse sendResponse = new SendResponse();
        sendResponse.setMsgId(message.getMsgId());
        sendResponse.setCode(0);
        WebSocketUtil.send(session, SendResponse.TYPE, sendResponse);

        //    创建转发的消息
        SendToUserReqeust sendToUserReqeust = new SendToUserReqeust();
        sendToUserReqeust.setMsgId(message.getMsgId());
        sendToUserReqeust.setContent(message.getContent());

        // 发送消息给指定用户
        WebSocketUtil.send(message.getToUser(), SendToUserReqeust.TYPE, sendToUserReqeust);

    }

    @Override
    public String getType() {
        return SendToOneRequest.TYPE;
    }
}
